package com.cn.customer.housekeeping.housekeepingc;


import android.app.TabActivity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.cn.customer.housekeeping.housekeepingc.bridge.BridgeFactory;
import com.cn.customer.housekeeping.housekeepingc.bridge.Bridges;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefManager;
import com.cn.customer.housekeeping.housekeepingc.bridge.cache.sharePref.EBSharedPrefUser;
import com.cn.customer.housekeeping.housekeepingc.citypicker.CityPickerActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.HomeListActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.MerchandiseListActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.MyActivity;
import com.cn.customer.housekeeping.housekeepingc.ui.ReleaseListActivity;
import com.cn.customer.housekeeping.housekeepingc.util.NavUtils;
import com.cn.customer.housekeeping.housekeepingc.util.StringUtil;
import com.cn.customer.housekeeping.housekeepingc.util.ToastUtil;


/***
 * 主页
 */
public class TabMainActivity extends TabActivity implements OnClickListener {
    private TabHost tabHost;
    private RelativeLayout tab_fl_no1, tab_fl_no2, tab_fl_no3, tab_fl_no4;
    private RelativeLayout rl_title;
    private ImageView tab_iv_no1, tab_iv_no2, tab_iv_no3, tab_iv_no4;
    private TextView tab_tv_no1, tab_tv_no2, tab_tv_no3, tab_tv_no4;
    private EBSharedPrefManager manager = BridgeFactory.getBridge(Bridges.SHARED_PREFERENCE);
    private TextView tv_top_city;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabmain);
        tabHost = this.getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent().setClass(this, HomeListActivity.class);
        spec = tabHost.newTabSpec("1").setIndicator("1").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, ReleaseListActivity.class);
        spec = tabHost.newTabSpec("2").setIndicator("2").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MerchandiseListActivity.class);
        spec = tabHost.newTabSpec("3").setIndicator("3").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MyActivity.class);
        spec = tabHost.newTabSpec("4").setIndicator("4").setContent(intent);
        tabHost.addTab(spec);
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tab_fl_no1 = (RelativeLayout) findViewById(R.id.tab_fl_no1);
        tab_fl_no2 = (RelativeLayout) findViewById(R.id.tab_fl_no2);
        tab_fl_no3 = (RelativeLayout) findViewById(R.id.tab_fl_no3);
        tab_fl_no4 = (RelativeLayout) findViewById(R.id.tab_fl_no4);

        tab_iv_no1 = (ImageView) findViewById(R.id.tab_iv_no1);
        tab_iv_no2 = (ImageView) findViewById(R.id.tab_iv_no2);
        tab_iv_no3 = (ImageView) findViewById(R.id.tab_iv_no3);
        tab_iv_no4 = (ImageView) findViewById(R.id.tab_iv_no4);

        tab_tv_no1 = (TextView) findViewById(R.id.tab_tv_no1);
        tab_tv_no2 = (TextView) findViewById(R.id.tab_tv_no2);
        tab_tv_no3 = (TextView) findViewById(R.id.tab_tv_no3);
        tab_tv_no4 = (TextView) findViewById(R.id.tab_tv_no4);

        tab_fl_no1.setOnClickListener(this);
        tab_fl_no2.setOnClickListener(this);
        tab_fl_no3.setOnClickListener(this);
        tab_fl_no4.setOnClickListener(this);
        tab_fl_no1.performClick();
        tabHost.setCurrentTab(0);

        //
        tv_top_city = (TextView) findViewById(R.id.tv_top_city);
        String city = manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.city, "");
        String cityChoice = manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.cityChoice, "");

        if (!StringUtil.isEmpty(cityChoice)) {
            tv_top_city.setText(cityChoice);
        } else if (!StringUtil.isEmpty(city)) {
            tv_top_city.setText(city);
        } else {
            tv_top_city.setText("未知");
        }

        tv_top_city.setOnClickListener(this);
    }


    @Override
    public void onClick(View arg0) {
        Resources resource = (Resources) getBaseContext().getResources();
        ColorStateList blackTv = (ColorStateList) resource.getColorStateList(R.color.colormain_fa5c5b);


        switch (arg0.getId()) {
            case R.id.tab_fl_no1://首页
                rl_title.setVisibility(View.VISIBLE);
                resetImg();

                tabHost.setCurrentTabByTag("1");
                tab_iv_no1.setBackgroundResource(R.mipmap.tabbar_icon_home_selected);
                tab_tv_no1.setTextColor(blackTv);
                break;
            case R.id.tab_fl_no2://发服务
                rl_title.setVisibility(View.GONE);
                resetImg();
                tabHost.setCurrentTabByTag("2");
                tab_iv_no2.setBackgroundResource(R.mipmap.tabbar_icon_serve_selected);
                tab_tv_no2.setTextColor(blackTv);
                break;
            case R.id.tab_fl_no3://订单
                rl_title.setVisibility(View.GONE);
                resetImg();
                tabHost.setCurrentTabByTag("3");
                tab_iv_no3.setBackgroundResource(R.mipmap.tabbar_icon_order_selected);
                tab_tv_no3.setTextColor(blackTv);

                break;
            case R.id.tab_fl_no4://我的
                rl_title.setVisibility(View.GONE);
                resetImg();
                tabHost.setCurrentTabByTag("4");
                tab_iv_no4.setBackgroundResource(R.mipmap.tabbar_icon_mine_selected);
                tab_tv_no4.setTextColor(blackTv);

                break;
            case R.id.tv_top_city://城市选择
                NavUtils.gotoCityPickerActivity(TabMainActivity.this, REQUEST_CODE_PICK_CITY);
                break;

            default:
                break;
        }
    }

    private static final int REQUEST_CODE_PICK_CITY = 233;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK) {
            if (data != null) {
                String cityChoice = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
                if (!StringUtil.isEmpty(cityChoice)) {
                    tv_top_city.setText("" + cityChoice);
                    String city = manager.getKDPreferenceUserInfo().getString(EBSharedPrefUser.city, "");
                    if (city.equals(cityChoice)) {
                        manager.getKDPreferenceUserInfo().saveString(EBSharedPrefUser.cityChoice, "");
                    } else {
                        manager.getKDPreferenceUserInfo().saveString(EBSharedPrefUser.cityChoice, cityChoice);
                    }
                }

            }
        }
    }

    /**
     * 把所有图片变暗
     */
    private void resetImg() {
        Resources resource = (Resources) getBaseContext().getResources();
        ColorStateList whiteHalf = (ColorStateList) resource.getColorStateList(R.color.black);
        tab_iv_no1.setBackgroundResource(R.mipmap.tabbar_icon_home_default);
        tab_iv_no2.setBackgroundResource(R.mipmap.tabbar_icon_serve_default);
        tab_iv_no3.setBackgroundResource(R.mipmap.tabbar_icon_order_default);
        tab_iv_no4.setBackgroundResource(R.mipmap.tabbar_icon_mine_default);
        if (whiteHalf != null) {
            tab_tv_no1.setTextColor(whiteHalf);
            tab_tv_no2.setTextColor(whiteHalf);
            tab_tv_no3.setTextColor(whiteHalf);
            tab_tv_no4.setTextColor(whiteHalf);
        }
    }

    long firstTime = 0;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 1000) {
                ToastUtil.makeText(this, "再点一次退出应用");
                firstTime = secondTime;//
            } else {
                finish();
            }
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}